﻿using AntdUI.In;
using Google.Protobuf.WellKnownTypes;
using Newtonsoft.Json.Linq;
using PosClient.Delegates;
using PosClient.Model;
using PosClient.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;

namespace PosClient.Controls
{
    public partial class PayGrid : Control, INotifyPropertyChanged
    {
        public PayGrid()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 索引
        /// </summary>
        public event GridIndexEventHandler GridIndex;
        /// <summary>
        /// 确认支付
        /// </summary>
        public event ConfirmPayEventHandler ConfirmPay;

        private int _Index;
        public int Index 
        {
            get => _Index;
            set 
            {
                if (_Index == value) return;
                _Index = value;
                NotifyPropertyChanged();
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Localizable(true)]
        [MergableProperty(false)]
        public BindingList<Pay> DataSource
        {
            get
            {
                return _DataSource;
            }
            set
            {
                if (_DataSource == value) return;
                _DataSource = value;
                ReloadList();
                NotifyPropertyChanged(nameof(_DataSource));
            }
        }
        private BindingList<Pay> _DataSource = new BindingList<Pay>();

        private void ReloadList()
        {
            flowPanel1.Controls.Clear();
            if (_DataSource.Count > 0)
            {
                for (int i = 0; i < _DataSource.Count; i++)
                {
                    var pay = _DataSource[i];
                    PayRow row = new PayRow();
                    row.Title = pay.PayName;
                    row.Index = i;
                    row.TagData = pay;
                    row.RowClick += RowChoose;
                    row.Svg = DataUtil.PayIconList.TryGetValue(pay.PayCode, out var svg) ? svg : DefaultIcon();
                    row.Size = row.Size.Height > ItemSize.Height ? row.Size : ItemSize;
                    flowPanel1.Controls.Add(row);
                }
                RowChoose(flowPanel1.Controls[0],null);
            }
        }

        private string DefaultIcon()
        {
            return "<svg t=\"1734423224094\" class=\"icon\" viewBox=\"0 0 1210 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"6860\" width=\"128\" height=\"128\"><path d=\"M568.847515 538.686061c-148.867879 0-268.474182-121.980121-268.474182-270.491152C300.373333 119.683879 422.632727 0.356848 568.847515 0.356848c148.852364 0 268.474182 121.980121 268.474182 267.838061 2.653091 148.51103-119.621818 270.506667-268.474182 270.506667z m0-501.201455c-127.596606 0-228.600242 103.424-228.600242 228.072727 0 127.286303 103.656727 230.710303 228.600242 230.710303 127.581091 0 228.600242-103.424 228.600243-230.710303 2.653091-124.648727-101.003636-228.072727-228.600243-228.072727z\" fill=\"#18b3d1\" p-id=\"6861\"></path><path d=\"M715.046788 114.377697L622.002424 273.501091h63.798303v47.740121h-79.747879v37.112243h79.747879v47.740121h-79.747879v66.296242h-74.426181v-66.296242h-85.054061v-47.740121h85.054061v-37.112243h-85.054061v-47.740121h69.104485L425.301333 114.362182h79.747879c34.552242 74.255515 58.476606 121.995636 66.451394 145.857939h2.653091c0-2.637576 2.668606-7.959273 5.321697-13.249939 2.668606-7.959273 13.296485-31.837091 34.552242-71.602424L640.620606 114.362182h74.426182zM21.255758 923.229091c-10.627879 0-21.255758-7.959273-21.255758-21.224727V610.304c2.653091-10.612364 10.627879-18.571636 21.255758-18.571636 10.643394 0 21.271273 7.959273 21.271272 21.209212v294.368969c0 7.959273-10.627879 15.918545-21.255757 15.918546z\" fill=\"#18b3d1\" p-id=\"6862\"></path><path d=\"M730.980848 838.361212l297.720243-111.383273c61.129697-23.862303 127.581091 7.959273 151.52097 68.949334l10.627878 23.877818-451.894303 161.76097c-82.385455 29.168485-170.108121 29.168485-255.177697 2.653091l-321.629091-114.036364V607.650909l63.798304-21.224727c71.773091-23.862303 146.199273-15.90303 217.956848 10.612363l366.824727 159.107879-69.104485 71.61794a103.330909 103.330909 0 0 1-36.615757 23.846788c-19.657697 7.338667-41.177212 7.912727-61.750303 0L478.487273 790.652121l198.019879 52.084364\" fill=\"#18b3d1\" p-id=\"6863\"></path><path d=\"M608.721455 1024c-42.542545 0-87.722667-7.959273-130.249697-21.209212l-324.297697-114.036364a19.952485 19.952485 0 0 1-13.296485-18.571636V607.650909c0-7.959273 5.321697-15.918545 13.296485-18.571636l63.798303-21.209212c71.773091-23.862303 148.852364-18.571636 231.253333 10.612363L816.050424 737.590303c5.321697 2.653091 10.627879 7.959273 10.627879 13.265455 0 5.290667 0 13.249939-5.306182 18.556121l-18.618182 18.571636 215.319273-79.561697c34.552242-13.265455 71.773091-13.265455 106.325333 2.637576 31.883636 15.918545 58.476606 42.433939 71.773091 76.924121l10.627879 23.862303c2.653091 5.306182 2.653091 10.612364 0 15.918546-2.653091 5.290667-5.321697 7.943758-10.627879 10.596848L744.261818 1000.137697C701.750303 1016.040727 656.554667 1024 608.721455 1024zM180.751515 856.932848l308.348121 108.730182c82.40097 26.515394 164.801939 23.862303 241.881212-2.653091l433.291637-153.817212-2.668606-5.306182c-10.627879-26.515394-29.230545-45.08703-53.154909-55.699393-23.924364-10.596848-50.501818-10.596848-74.426182-2.637576l-295.067152 111.383272a105.735758 105.735758 0 0 1-101.003636 13.24994l-164.801939-60.974546c-10.627879-2.668606-15.949576-15.934061-10.627879-26.530909 2.653091-10.612364 15.949576-15.918545 26.577454-10.612363l164.80194 60.99006c26.577455 10.612364 55.823515 2.653091 77.079272-18.556121l47.864243-50.393212-340.247273-148.51103c-74.441697-26.515394-140.877576-29.168485-204.675879-10.596849l-53.170424 15.90303v236.032zM871.377455 406.093576c5.306182 0 7.974788-2.637576 13.280969-5.290667l151.52097-119.342545c5.306182-2.653091 7.974788-10.612364 7.974788-15.918546 0-5.290667-2.668606-13.249939-7.974788-15.90303l-151.52097-114.036364c-7.974788-7.959273-21.255758-5.306182-26.577454 2.637576-5.321697 7.974788-5.321697 21.224727 2.653091 29.168485l130.249697 98.133333-130.249697 103.424c-7.974788 7.959273-10.627879 18.571636-2.653091 29.168485 2.653091 7.959273 7.974788 7.959273 13.296485 7.959273z\" fill=\"#18b3d1\" p-id=\"6864\"></path></svg>";

        }

        public void GridKeyDown(Keys e)
        {
            if (e == Keys.Enter)
            {
                Confirm();
            }
        }

        public void GridKeyUp(Keys e)
        {
            if (e == Keys.Left)
            {
                if (_Index > 0 && _Index < flowPanel1.Controls.Count)
                {
                    RowChoose(flowPanel1.Controls[_Index - 1], null);
                }
            }
            else if (e == Keys.Right)
            {
                if (_Index < flowPanel1.Controls.Count - 1)
                {
                    RowChoose(flowPanel1.Controls[_Index + 1], null);
                }
            }
        }

        /// <summary>
        /// 选中支付方式
        /// </summary>
        private void RowChoose(object sender, EventArgs e)
        {
            foreach (var control in flowPanel1.Controls)
            {
                if (control is PayRow)
                {
                    (control as PayRow).Check = false;
                }
            }
            var activeControl = (sender as PayRow);
            _Index = activeControl.Index;
            GridIndex?.Invoke(activeControl, e);//通知更新索引
            activeControl.Check = true;
            flowPanel1.ScrollControlIntoView(activeControl);
        }

        /// <summary>
        /// 确认使用此支付
        /// </summary>
        private void Confirm()
        {
            ConfirmPay?.Invoke(flowPanel1.Controls[_Index], null);
        }

        [Description("ItemWith"), Category("外观")]
        public Size ItemSize
        {
            get => _ItemSize;
            set
            {
                if (_ItemSize == value) return;
                _ItemSize = value;
                ReloadList();
                Invalidate();
                NotifyPropertyChanged();
            }
        }
        private Size _ItemSize;

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); //实现通知刷新
        }
    }
}
